package com.clayecloud.module_visitor;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.module_visitor.adapter.VisitorListAdapter;
import com.clayecloud.module_visitor.databinding.ActivityVisitorListBinding;
import com.clayecloud.module_visitor.entity.Visitor;
import com.clayecloud.module_visitor.viewModels.VisitorListViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;

import java.util.List;

@Route(path = "/visitor/list/index")
public class VisitorListActivity extends AppCompatActivity implements ToastAction {

    private Context context = this;

    private ActivityVisitorListBinding activityVisitorListBinding;

    private VisitorListViewModel visitorListViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityVisitorListBinding = DataBindingUtil.setContentView(this, R.layout.activity_visitor_list);
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        visitorListViewModel = viewModelProvider.get(VisitorListViewModel.class);
        // 初始化函数
        init();
        // 绑定监听事件
        initListener();
    }

    private void init() {
        // 控制状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        visitorListViewModel.leaddata();
        activityVisitorListBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initListener() {
        activityVisitorListBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
                overridePendingTransition(R.anim.window_fake, R.anim.window_right_out);
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });

        visitorListViewModel.getVisitors().observe(this, new Observer<List<Visitor>>() {
            @Override
            public void onChanged(List<Visitor> visitors) {
                VisitorListAdapter visitorListAdapter = new VisitorListAdapter(context, visitors);
                activityVisitorListBinding.recyclerView.setAdapter(visitorListAdapter);
                setItemListener(visitorListAdapter);
            }
        });
    }

    private void setItemListener(VisitorListAdapter visitorListAdapter){
        visitorListAdapter.setOnItemClickListener(new VisitorListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Visitor item) {
                // 跳转进行 二维码显示等
            }
        });
    }

    /**
     * 在删除此Activity时 进行操作防止内存泄漏
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.destroy(this, null);
    }

}